package com.seanliao.nav.controller;

import com.seanliao.nav.common.domain.dto.RoleDTO;
import com.seanliao.nav.common.domain.dto.valid.Add;
import com.seanliao.nav.common.domain.dto.valid.Edit;
import com.seanliao.nav.common.domain.vo.RoleVO;
import com.seanliao.nav.common.response.Result;
import com.seanliao.nav.common.response.ResultBuilder;
import com.seanliao.nav.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private ISysRoleService roleService;

    /**
     * 获取所有角色
     */
    @GetMapping("/listAll")
    @PreAuthorize("@pms.hasPermission('role:list')")
    public Result<List<RoleVO>> listAll() {
        List<RoleVO> roleVoList = roleService.listAll();
        return ResultBuilder.success(roleVoList);
    }

    /**
     * 根据用户id获取角色列表
     */
    @GetMapping("/listByUserId")
    @PreAuthorize("@pms.hasPermission('role:listByUserId')")
    public Result<List<RoleVO>> listByUserId(@RequestParam Long userId) {
        List<RoleVO> roleVoList = roleService.listByUserId(userId);
        return ResultBuilder.success(roleVoList);
    }

    /**
     * 添加角色
     */
    @PostMapping("/add")
    @PreAuthorize("@pms.hasPermission('role:add')")
    public Result<?> add(@Validated(Add.class) @RequestBody RoleDTO roleDTO) {
        roleService.add(roleDTO);
        return ResultBuilder.success();
    }

    /**
     * 编辑角色
     */
    @PostMapping("/edit")
    @PreAuthorize("@pms.hasPermission('role:edit')")
    public Result<?> edit(@Validated(Edit.class) @RequestBody RoleDTO roleDTO) {
        roleService.edit(roleDTO);
        return ResultBuilder.success();
    }

    /**
     * 删除角色
     */
    @PostMapping("/remove")
    @PreAuthorize("@pms.hasPermission('role:remove')")
    public Result<?> remove(@RequestParam Long roleId) {
        roleService.remove(roleId);
        return ResultBuilder.success();
    }

    /**
     * 排序
     */
    @PostMapping("/sort")
    @PreAuthorize("@pms.hasPermission('role:sort')")
    public Result<?> sort(@Validated(Edit.class) @RequestBody List<RoleDTO> roleDTOList) {
        roleService.sort(roleDTOList);
        return ResultBuilder.success();
    }

    /**
     * 根据用户分配角色
     */
    @PostMapping("/allocateByUser")
    @PreAuthorize("@pms.hasPermission('role:allocate')")
    public Result<?> editUserRole(@RequestParam Long userId,
                                  @RequestParam List<Long> roleIds) {
        roleService.editUserRole(userId, roleIds);
        return ResultBuilder.success();
    }

    /**
     * 根据角色分配角色
     */
    @PostMapping("/allocateByRole")
    @PreAuthorize("@pms.hasPermission('role:allocate')")
    public Result<?> editRoleUser(@RequestParam Long roleId,
                                  @RequestParam List<Long> userIds) {
        roleService.editRoleUser(roleId, userIds);
        return ResultBuilder.success();
    }

    /**
     * 添加角色用户
     * 类似于分配角色，但可单个处理
     */
    @PostMapping("/addUserRole")
    @PreAuthorize("@pms.hasPermission('role:allocate')")
    public Result<?> addUserRole(@RequestParam Long userId,
                                 @RequestParam List<Long> roleIds) {
        roleService.editUserRole(userId, roleIds);
        return ResultBuilder.success();
    }


}
